<%--
  Created by IntelliJ IDEA.
  User: zuxia
  Date: 2018/8/20
  Time: 11:08
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>发布需求</title>
    <link rel="stylesheet" href="../frame/layui/css/layui.css">
    <link rel="stylesheet" href="../frame/static/css/style.css">
</head>
<body class="body">

<fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
    <legend>发布需求</legend>
</fieldset>

<form class="layui-form">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">招聘部门</label>
            <div class="layui-input-inline">
                <select id="depId" lay-filter="depId" name="depId" lay-verify="required">
                    <option value="">请选择部门</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">招聘人数</label>
            <div class="layui-input-inline" style="width: 100px;">
                <input type="text" name="appNum" lay-verify="number" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">招聘职位</label>
            <div class="layui-input-inline">
                <select id="posId" name="posId" lay-verify="required">
                    <option value="">请选择职位</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">开始日期</label>
            <div class="layui-input-inline">
                <input type="text" name="appTimeBegin" id="date" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">截止日期</label>
            <div class="layui-input-inline">
                <input type="text" name="appTimeEnd" id="date1" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">工作地点</label>
            <div class="layui-input-inline">
                <select name="appLand" lay-verify="required" lay-search="">
                    <option value="">直接选择或搜索选择</option>
                    <option value='湖北'>湖北</option>
                    <option value='广东'>广东</option>
                    <option value='江西'>江西</option>
                    <option value='安徽'>安徽</option>
                    <option value='福建'>福建</option>
                    <option value='广西'>广西</option>
                    <option value='云南'>云南</option>
                    <option value='四川'>四川</option>
                    <option value='贵州'>贵州</option>
                    <option value='湖南'>湖南</option>
                    <option value='浙江'>浙江</option>
                    <option value='江苏'>江苏</option>
                    <option value='河南'>河南</option>
                    <option value='河北'>河北</option>
                    <option value='山东'>山东</option>
                    <option value='山西'>山西</option>
                    <option value='陕西'>陕西</option>
                    <option value='甘肃'>甘肃</option>
                    <option value='青海'>青海</option>
                    <option value='宁夏'>宁夏</option>
                    <option value='内蒙古自治区'>内蒙古自治区</option>
                    <option value='辽宁'>辽宁</option>
                    <option value='吉林'>吉林</option>
                    <option value='黑龙江'>黑龙江</option>
                    <option value='新疆自治区'>新疆自治区</option>
                    <option value='西藏自治区'>西藏自治区</option>
                    <option value='海南'>海南</option>
                    <option value='澳门'>澳门</option>
                    <option value='香港'>香港</option>
                    <option value='台湾'>台湾</option>
                    <option value='北京市'>北京市</option>
                    <option value='天津市'>天津市</option>
                    <option value='上海'>上海</option>
                    <option value='重庆市'>重庆市</option>
                </select>
            </div>
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" lay-filter="demo1">发布</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>
</form>
<script src="../js/jquery.js"></script>
<script src="../frame/layui/layui.js" charset="utf-8"></script>
<script>

    $(function () {
        // 加载部门
        $.ajax({
            url: "/selectAllDep",
            type: "POST",
            dataType:"json",
            success: function(data){
                if(data.success){
                    var department = data.data;
                    // 判断是否有数据
                    if(department.length>=0) {
                        for (var i = 0; i < department.length; i++) {
                            $("#depId").append("<option value='"+department[i].depid+"'>"+department[i].depname+"</option>");
                        }
                    }else{
                        layer.msg("未查询到数据！");
                    }
                }else{
                    // 错误提示
                    layer.msg(data.message);
                }
            }
        });

    });

    // 根据选择的部门加载职位
    function loadPositi(form) {
        var depid = $("#depId").val();
        $.ajax({
            url: "/selectDepPosition",
            type: "POST",
            data: {
                department_id: depid
            },
            dataType: "json",
            success: function (data) {
                $("#posId").empty();
                if (data.success) {
                    var positi = data.data;
                    // 判断是否有数据
                    if (positi.length >= 0) {
                        for (var i = 0; i < positi.length; i++) {
                            $("#posId").append("<option value='" + positi[i].posid + "'>" + positi[i].posname + "</option>");
                        }
                    } else {
                        layer.msg("未查询到数据！");
                    }
                    form.render('select');
                } else {
                    // 错误提示
                    layer.msg(data.message);
                }
            }
        });
    }

    // 时间控件
    layui.use(['form', 'layedit', 'laydate'], function(){
        var form = layui.form
            ,laydate = layui.laydate;

        //日期
        laydate.render({
            elem: '#date'
        });
        laydate.render({
            elem: '#date1'
        });
        // 表单提交校验
        form.on('submit(demo1)', function(data){
            add(data);
            return false;
        });
        form.render('select');
        form.on('select(depId)', function(data){
            loadPositi(form);
        });

    });
    // 添加数据
    function add(data){
        // 获取需要的值
        var posid = data.field.posId;
        posid = parseInt(posid);
        var appnum = data.field.appNum;
        appnum = parseInt(appnum);
        var depid = data.field.depId;
        depid = parseInt(depid);
        var apptimebegin = data.field.appTimeBegin;
        apptimebegin = new Date(apptimebegin);
        var apptimeend = data.field.appTimeEnd;
        apptimeend = new Date(apptimeend);
        var appland = data.field.appLand;
        $.ajax({
            url: "/Application/add",
            type: "POST",
            dataType:"json",
            data: {
                "positi.posid":posid,
                appnum:appnum,
                "department.depid":depid,
                apptimebegin:apptimebegin,
                apptimeend:apptimeend,
                appland:appland
            },
            success: function(data){
                if(data.success){
                    layer.confirm(data.message+"是否继续添加？", {
                        btn: ['继续', '取消'] //按钮
                    }, function(){
                        window.location.reload();
                    }, function(){
                        // 跳转到展示页面
                        window.location.href="applicationList.jsp"
                    });
                }else {
                    layer.msg(data.message);
                }
            }
        });
    }
</script>
</body>
</html>
